<?php

namespace App\Admin\Controllers;

use App\Models\Category;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class CategoriesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('分类');
            $content->body(Category::tree(function ($tree) {
                $tree->branch(function ($branch) {
                    return "<span class='label label-sm label-danger margin-r-5'>{$branch['name']}</span> <span class='label label-sm label-default margin-r-5'>热门排序：{$branch['hot']}</span>" . "<span class='label label-sm label-default margin-r-5'>首页排序：{$branch['home']}</span>";
                });
            }));
        });

    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('');

            $content->body($this->form());
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Category::class, function (Form $form) {

            $form->text('name','名称');
            $form->select('parent_id','上级')->options(Category::selectOptions());
            $form->number('hot','热门排序')->help('越大越靠前');
            $form->number('home','首页排序')->help('越大越靠前');
        });
    }
}
